package com.sprouting.ops.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import com.sprouting.ops.interfaces.AddGroup;
import com.sprouting.ops.interfaces.ConstantBase;
import com.sprouting.ops.interfaces.UpdGroup;
import com.sprouting.ops.model.DromaraFileStorage;
import com.sprouting.ops.model.OpsThirdParty;
import com.sprouting.ops.model.ResultJson;
import com.sprouting.ops.model.qo.OpsThirdPartyQo;
import com.sprouting.ops.model.vo.OpsThirdPartyVo;
import com.sprouting.ops.service.DromaraFileStorageService;
import com.sprouting.ops.service.OpsThirdPartyService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.EntityTool;
import com.sprouting.ops.utils.IdTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 第三方厂商与客户
 *
 * @author ：LX（长沙麓谷）
 * @date 创建时间： 2023/11/10 15:43
 */
@Slf4j
@RestController
@RequestMapping("third")
public class OpsThirdPartyController {

    @Resource
    private OpsThirdPartyService opsThirdPartyService;
    @Resource
    private DromaraFileStorageService dromaraFileStorageService;


    /**
     * 逻辑删除厂商
     * @param thirdPartyId 厂商id
     * @return
     */
    @IdempotentSingle
    @PostMapping("delThirdParty")
    public ResultJson delThirdParty(String thirdPartyId){
        if (CheckTool.checkNull(thirdPartyId)){
            return ResultJson.errorCheck();
        }

        OpsThirdParty opsThirdParty = opsThirdPartyService.getById(thirdPartyId);
        if (opsThirdParty == null){
            return ResultJson.success();
        }

        DromaraFileStorage opsUploadFile = dromaraFileStorageService.delFileBefore(opsThirdParty.getIco());
        // 逻辑删除
        opsThirdPartyService.delThirdParty(opsThirdParty, opsUploadFile);
        return ResultJson.success();
    }

    /**
     * 分页查询
     * @param query 查询参数
     * @return
     */
    @PostMapping("getThirdPartyPageList")
    public ResultJson getThirdPartyPageList(OpsThirdPartyQo query){
        Page page = query.getPage();
        Page<OpsThirdPartyVo> thirdPartyPageList = opsThirdPartyService.getThirdPartyPageList(page, query);
        return ResultJson.success(thirdPartyPageList);
    }

    /**
     * 保存 第三方
     * @param opsThirdParty 第三方
     * @return
     */
    @IdempotentSingle
    @PostMapping("saveThirdParty")
    public ResultJson saveThirdParty(@Validated(AddGroup.class) OpsThirdParty opsThirdParty){
        String msg = checkThirdParty(opsThirdParty);
        if (msg != null){
            return ResultJson.error(msg);
        }

        opsThirdParty.setId(IdTool.getId());
        EntityTool.saveBase(opsThirdParty);

        // 处理图片
        DromaraFileStorage opsUploadFile = dromaraFileStorageService.toProductionBefore(opsThirdParty.getIco(), opsThirdParty.getId(), ConstantBase.FileSourceType.THIRD_PARTY_ICO);

        opsThirdPartyService.saveThirdParty(opsThirdParty, opsUploadFile);
        return ResultJson.success(opsThirdParty);
    }

    /**
     * 更新 第三方
     * @param opsThirdParty 第三方
     * @return
     */
    @IdempotentSingle
    @PostMapping("updThirdParty")
    public ResultJson updThirdParty(@Validated(UpdGroup.class) OpsThirdParty opsThirdParty){
        String msg = checkThirdParty(opsThirdParty);
        if (msg != null){
            return ResultJson.error(msg);
        }

        EntityTool.updBase(opsThirdParty);

        // 处理图片
        OpsThirdParty oldDb = opsThirdPartyService.getById(opsThirdParty.getId());
        if (oldDb == null){
            return ResultJson.errorCheck();
        }
        DromaraFileStorage[] uploadFileArray = dromaraFileStorageService.updFileBefore(opsThirdParty.getIco(), oldDb.getIco(), opsThirdParty.getId(), ConstantBase.FileSourceType.THIRD_PARTY_ICO);

        opsThirdPartyService.updThirdParty(opsThirdParty, uploadFileArray);
        return ResultJson.success(opsThirdParty);
    }

    /**
     * 检查 第三方
     * @param opsThirdParty 第三方
     * @return 非null则有异常
     */
    private String checkThirdParty(OpsThirdParty opsThirdParty){
        OpsThirdParty dbName = opsThirdPartyService.getByNameFilterId(opsThirdParty.getName(), opsThirdParty.getId());
        if (dbName != null){
            return "名称已使用，请勿重复添加";
        }

        return null;
    }

}
